<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px" class="bg-white p-1">
      <el-form-item label="流程名称" prop="dictName">
        <el-input
            v-model="queryParams.query.dictName"
            placeholder="请输入流程名称"
            clearable
            size="small"
            style="width: 240px"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="流程类型" prop="dictType">
        <el-select v-model="queryParams.query.processTypeCode" placeholder="请选择类型" clearable style="width:130px">
          <el-option v-for='item in processTypeDictList' :label="item.dictLabel" :value="item.dictValue"
                     :key="item.dictValue"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8 my-1">
      <el-col :span="1.5">
        <div class="flex flex-x">
          <div class="fillBtn" @click="handleAdd" v-hasPermi="['system:user:add']">
            <img src="@/assets/images/gzq/icon-add.png" alt=""/>
            <span>新增数据</span>
          </div>
        </div>
      </el-col>
    </el-row>

    <el-table v-loading="searchLoading" :data="processList" :header-cell-style="headerStyle" :cell-style="cellStyle">
      <el-table-column label="流程名称" align="center" prop="name"  width="220"/>
      <el-table-column label="流程类型" align="center" width="120">
        <template slot-scope="scope">
          <el-button type="text">{{ scope.row.processTypeName }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="节点" align="center" prop="nodeUserNames" :show-overflow-tooltip="true"/>
      <el-table-column label="抄送人" align="center" prop="csUserNames" />
      <el-table-column label="流程描述" align="center" prop="description" width="280"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="300">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:dict:edit']"
          >修改
          </el-button>
          <el-button
              size="mini"
              type="text"
              style="color: red"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:dict:remove']"
          >删除
          </el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-setting"
              @click="handleNoteConfig(scope.row)"
              v-hasPermi="['system:dict:edit']"
          >配置节点
          </el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-user"
              @click="handleCsConfig(scope.row)"
              v-hasPermi="['system:dict:edit']"
          >配置抄送人
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="流程名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入流程名称"/>
        </el-form-item>
        <el-form-item label="流程类型" prop="processTypeCode">
          <el-select v-model="form.processTypeCode" placeholder="请选择类型" clearable style="width:200px">
            <el-option v-for='item in processTypeDictList' :label="item.dictLabel" :value="item.dictValue"
                       :key="item.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="流程描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-drawer
        title="审批节点配置"
        :visible.sync="nodeDrawer"
        direction="rtl"
        size="800px"
        :with-header="false"
    >
      <div class="font-small m-1 font-weight-bold">审批节点配置</div>
      <el-row :gutter="10" class="mb8 my-1">
        <el-col :span="1.5">
          <div class="flex flex-x">
            <div class="fillBtn" @click="handleAddNode" v-hasPermi="['system:user:add']">
              <img src="@/assets/images/gzq/icon-add.png" alt=""/>
              <span>添加节点</span>
            </div>
          </div>
        </el-col>
      </el-row>
      <el-table v-loading="searchLoadingNote" :data="processNodeList" :header-cell-style="headerStyle"
                :cell-style="cellStyle">
        <el-table-column label="排序" align="center" prop="orderNum" width="100px"/>
        <el-table-column label="节点类型" align="center" prop="processNodeTypeName" :show-overflow-tooltip="true"/>
        <el-table-column label="标的" align="center" prop="targetName" :show-overflow-tooltip="true"/>
        <el-table-column label="参与非网际项目" align="center" prop="ifJoinNoWjProject">
          <template slot-scope="scope">
            <el-button type="text"  v-if="scope.row.ifJoinNoWjProject==1">是</el-button>
            <el-button type="text" style="color: red" v-if="scope.row.ifJoinNoWjProject==0">否</el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleEditNode(scope.row)"
                v-hasPermi="['system:dict:edit']"
            >修改
            </el-button>
            <el-button
                size="mini"
                type="text"
                style="color: red"
                icon="el-icon-delete"
                @click="handleDeleteNode(scope.row)"
                v-hasPermi="['system:dict:remove']"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-drawer>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="nodeTitle" :visible.sync="nodeOpen" width="500px" append-to-body>
      <el-form ref="nodeForm" :model="nodeForm" :rules="nodeRules" label-width="150px">
        <el-form-item label="节点类型" prop="processNodeTypeCode">
          <el-select v-model="nodeForm.processNodeTypeCode" placeholder="请选择类型" clearable style="width:200px">
            <el-option v-for='item in processNodeTypeDictList' :label="item.dictLabel" :value="item.dictValue"
                       :key="item.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标的" prop="targetId" v-if="nodeForm.processNodeTypeCode==='zdyh'">
          <el-select v-model="nodeForm.targetId" placeholder="请选择用户" clearable style="width:200px">
            <el-option v-for='item in userList' :label="item.name" :value="item.id" :key="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="参与非网际项目">
          <el-radio-group v-model="nodeForm.ifJoinNoWjProject">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="顺序" prop="orderNum">
          <el-input-number v-model="nodeForm.orderNum" controls-position="right" :min="1"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormNode">确 定</el-button>
        <el-button @click="cancelNode">取 消</el-button>
      </div>
    </el-dialog>


    <el-drawer
        title="抄送人配置"
        :visible.sync="csDrawer"
        direction="rtl"
        size="800px"
        :with-header="false"
    >
      <div class="font-small m-1 font-weight-bold">抄送人配置</div>
      <el-row :gutter="10" class="mb8 my-1">
        <el-col :span="1.5">
          <div class="flex flex-x">
            <div class="fillBtn" @click="handleAddCs" v-hasPermi="['system:user:add']">
              <img src="@/assets/images/gzq/icon-add.png" alt=""/>
              <span>添加抄送人</span>
            </div>
          </div>
        </el-col>
      </el-row>
      <el-table v-loading="searchLoadingCs" :data="processCsList" :header-cell-style="headerStyle"
                :cell-style="cellStyle">
        <el-table-column label="序号" align="center"  type="index" width="100px"/>
        <el-table-column label="抄送人" align="center" prop="userName" :show-overflow-tooltip="true"/>
        <el-table-column label="参与非网际项目" align="center" prop="ifJoinNoWjProject">
          <template slot-scope="scope">
            <el-button type="text"  v-if="scope.row.ifJoinNoWjProject==1">是</el-button>
            <el-button type="text" style="color: red" v-if="scope.row.ifJoinNoWjProject==0">否</el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleEditCs(scope.row)"
                v-hasPermi="['system:dict:edit']"
            >修改
            </el-button>
            <el-button
                size="mini"
                type="text"
                style="color: red"
                icon="el-icon-delete"
                @click="handleDeleteCs(scope.row)"
                v-hasPermi="['system:dict:remove']"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-drawer>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="csTitle" :visible.sync="csOpen" width="500px" append-to-body>
      <el-form ref="csForm" :model="csForm" :rules="csRules" label-width="150px">
        <el-form-item label="抄送人" prop="userId">
          <el-select v-model="csForm.userId" placeholder="请选择用户" clearable style="width:200px">
            <el-option v-for='item in userList' :label="item.name" :value="item.id" :key="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="参与非网际项目">
          <el-radio-group v-model="csForm.ifJoinNoWjProject">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormCs">确 定</el-button>
        <el-button @click="cancelCs">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  auditProcessPage,
  auditProcessDetail,
  auditProcessAdd,
  auditProcessUpdate,
  auditProcessDelete
} from "@/api/sys/auditProcess";

import {
  auditProcessNodeList,
  auditProcessNodeDetail,
  auditProcessNodeAdd,
  auditProcessNodeUpdate,
  auditProcessNodeDelete
} from "@/api/sys/auditProcessNode";

import {
  auditProcessCsList,
  auditProcessCsDetail,
  auditProcessCsAdd,
  auditProcessCsUpdate,
  auditProcessCsDelete
} from "@/api/sys/auditProcessCs";

import {userListNoAdmin} from "@/api/sys/user";

export default {
  name: "DictType",
  data() {
    return {
      nodeDrawer: false, //配置节点抽屉开关
      csDrawer: false, //抄送人抽屉开关
      searchLoading: true,
      processList: [],
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      form: {},
      processTypeDictList: [],
      rules: {
        name: [
          {required: true, message: "流程名称不能为空", trigger: "blur"}
        ],
        processTypeCode: [
          {required: true, message: "流程类型不能为空", trigger: "blur"}
        ]
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        query: {
          name: undefined,
          processTypeCode: undefined
        }
      },
      userList: [],
      nodeTitle: "",
      nodeOpen: false,
      csTitle: "",
      csOpen: false,
      searchLoadingNote: true,
      searchLoadingCs: true,
      processNodeList: [],
      processCsList: [],
      queryParamsNode: {
        query: {
          processNodeType: undefined,
        }
      },
      nodeForm: {
        ifJoinNoWjProject:1,
        orderNum:1
      },
      csForm: {
        ifJoinNoWjProject:1,
      },
      processNodeTypeDictList: [],
      nodeRules: {
        processNodeTypeCode: [
          {required: true, message: "流程节点类型不能为空", trigger: "blur"}
        ],
        orderNum: [
          {required: true, message: "流程节点顺序不能为空", trigger: "blur"}
        ]
      },
      csRules: {
        userId: [
          {required: true, message: "抄送人不能为空", trigger: "blur"}
        ],
      },
    }
  },
  created() {
    this.getDicts("audit_process_type").then(response => {
      this.processTypeDictList = response.data;
    });
    this.getDicts("audit_process_node_type").then(response => {
      this.processNodeTypeDictList = response.data;
    });
    this.getList();
    this.getUserList();
  },
  methods: {
    toDictData(dickType) {
      this.$router.push({
        path: "/sys/dict/data",
        query: {
          dickType: dickType
        }
      })
    },
    getList() {
      this.searchLoading = true;
      auditProcessPage(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.processList = response.data.rows;
        this.total = response.data.total;
        this.searchLoading = false;
      });
    },
    getUserList() {
      userListNoAdmin({}).then(response => {
        this.userList = response.data;
      });
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加流程类型";
    },
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      auditProcessDetail(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改流程类型";
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除该数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return auditProcessDelete(row.id);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id !== undefined) {
            auditProcessUpdate(this.form).then(() => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            auditProcessAdd(this.form).then(() => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        dictName: undefined,
        dictType: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    handleNoteConfig(row) {
      this.nodeDrawer = true;
      this.nodeForm.processId = row.id;
      this.getNodeList(row.id)
    },

    handleCsConfig(row) {
      this.csDrawer = true;
      this.csForm.processId = row.id;
      this.getCsList(row.id)
    },

    getNodeList(processId) {
      this.searchLoadingNote = true;
      auditProcessNodeList({"processId": processId}).then(response => {
        this.processNodeList = response.data;
        this.searchLoadingNote = false;
      });
    },

    getCsList(processId) {
      this.searchLoadingCs = true;
      auditProcessCsList({"processId": processId}).then(response => {
        this.processCsList = response.data;
        this.searchLoadingCs = false;
      });
    },

    handleAddNode() {
      this.nodeTitle = "添加节点";
      this.nodeOpen = true;
      this.resetNode();
    },

    handleAddCs() {
      this.csTitle = "添加抄送人";
      this.csOpen = true;
      this.resetCs();
    },

    handleEditNode(row) {
      this.nodeForm = {
        id: row.id,
        processId: row.processId,
        processNodeTypeCode: row.processNodeTypeCode,
        targetId: row.targetId,
        orderNum: row.orderNum,
        ifJoinNoWjProject: row.ifJoinNoWjProject
      };
      this.nodeTitle = "编辑节点";
      this.nodeOpen = true;
    },

    handleEditCs(row) {
      this.csForm = {
        id: row.id,
        processId: row.processId,
        userId: row.userId,
        ifJoinNoWjProject: row.ifJoinNoWjProject
      };
      this.csTitle = "编辑节点";
      this.csOpen = true;
    },

    handleDeleteNode(row) {
      this.$confirm('是否确认删除该节点么?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return auditProcessNodeDelete(row.id);
      }).then(() => {
        this.getNodeList(row.processId)
        this.msgSuccess("删除成功");
      })
    },

    handleDeleteCs(row) {
      this.$confirm('是否确认删除该抄送人么?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return auditProcessCsDelete(row.id);
      }).then(() => {
        this.getCsList(row.processId)
        this.msgSuccess("删除成功");
      })
    },
    cancelNode() {
      this.nodeOpen = false;
      this.resetNode();
    },

    cancelCs() {
      this.csOpen = false;
      this.resetCs();
    },

    // 表单重置
    resetNode() {
      this.nodeForm = {
        id: undefined,
        processId: this.nodeForm.processId,
        processNodeTypeCode: undefined,
        targetId: undefined,
        orderNum: undefined,
        ifJoinNoWjProject: 1
      };
      this.resetForm("nodeForm");
    },

    // 表单重置
    resetCs() {
      this.csForm = {
        id: undefined,
        processId: this.csForm.processId,
        userId: undefined,
        ifJoinNoWjProject: 1
      };
      this.resetForm("csForm");
    },

    submitFormNode: function () {
      this.$refs["nodeForm"].validate(valid => {
        if (valid) {
          if (this.nodeForm.id !== undefined) {
            auditProcessNodeUpdate(this.nodeForm).then(() => {
              this.msgSuccess("修改成功");
              this.nodeOpen = false;
              this.getNodeList(this.nodeForm.processId);
              this.resetNode();
            });
          } else {
            auditProcessNodeAdd(this.nodeForm).then(() => {
              this.msgSuccess("新增成功");
              this.nodeOpen = false;
              this.getNodeList(this.nodeForm.processId);
              this.resetNode();
            });
          }
        }
      });
    },

    submitFormCs: function () {
      this.$refs["csForm"].validate(valid => {
        if (valid) {
          if (this.csForm.id !== undefined) {
            auditProcessCsUpdate(this.csForm).then(() => {
              this.msgSuccess("修改成功");
              this.csOpen = false;
              this.getCsList(this.csForm.processId);
              this.resetCs();
            });
          } else {
            auditProcessCsAdd(this.csForm).then(() => {
              this.msgSuccess("新增成功");
              this.csOpen = false;
              this.getCsList(this.csForm.processId);
              this.resetCs();
            });
          }
        }
      });
    },

    headerStyle() {
      return 'background:#f7f8fa;color: black;font-size: 16px; height: 36px; border:1px solid #1e6fff0d;'
    },
    cellStyle({row, column, rowIndex}) {
      if (rowIndex % 2 === 0) {
        return 'background: #FFF!important;border:1px solid #1e6fff0d;line-height: 42px;height:42px;color: black;font-size: 14px;'
      } else {
        return 'background: #FFF!important;border:1px solid #1e6fff0d;line-height: 36px;height: 36px;color: black;font-size: 14px;'
      }
    },
  }
}
</script>

<style scoped>


</style>
